package com.wl156.activities;

import java.io.IOException;

import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.xmlpull.v1.XmlPullParserException;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.wl156.MyApp;
import com.wl156.lite.R;
import com.wl156.model.Certificate;
import com.wl156.model.WebServiceClient;

public class Register extends Activity implements OnClickListener {

	Button mConfirm;
	// Button mActive;
	// EditText mUsername;
	EditText mPassword;
	EditText mPasswordConfirm;
	// EditText mName;
	EditText mPhone;
	RadioGroup mUserType;

	private WebServiceClient mWebServiceClient;
	private Certificate mCertificate;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.act_register);

		mWebServiceClient = new WebServiceClient(this);
		mCertificate = ((MyApp) getApplication()).getCertificateFromXml();

		mConfirm = (Button) findViewById(R.id.confirm);
		mConfirm.setOnClickListener(this);
		// mActive = (Button) findViewById(R.id.active);
		// mActive.setOnClickListener(this);
		mPassword = (EditText) findViewById(R.id.password);
		mPasswordConfirm = (EditText) findViewById(R.id.passwordConfirm);
		mPhone = (EditText) findViewById(R.id.phone);
		mUserType = (RadioGroup) findViewById(R.id.user_type);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.confirm:
			
			final String password = mPassword.getText().toString();
			final String passwordConfirm = mPasswordConfirm.getText()
					.toString();
			if (!password.equals(passwordConfirm)) {
				Toast.makeText(this, "两次输入密码不一致", Toast.LENGTH_LONG).show();
				return;
			}
			// final String name = mName.getText().toString();
			final String phone = mPhone.getText().toString();
			final String username = phone;
			final String type = mUserType.getCheckedRadioButtonId() == R.id.bus ? "D"
					: "C";

			new AsyncTask<Void, Void, Object>() {
				ProgressDialog mDialog;

				@Override
				protected void onPreExecute() {
					mDialog = ProgressDialog.show(Register.this, "", "注册中...");
				};

				@Override
				protected void onPostExecute(Object result) {
					mDialog.dismiss();

					if (result == null) {
						Toast.makeText(Register.this, "注册成功", Toast.LENGTH_LONG)
								.show();
						((MyApp) getApplication()).putUserName(username);

						Intent i = new Intent(Register.this, Login.class);
						i.putExtra("username", username);
						i.putExtra("password", password);
						startActivity(i);
						finish();
					}

					if (result instanceof SoapPrimitive) {
						SoapPrimitive msg = (SoapPrimitive) result;
						Toast.makeText(Register.this, msg.toString(),
								Toast.LENGTH_LONG).show();
					} else if (result instanceof SoapObject) {
						Toast.makeText(Register.this, "注册成功", Toast.LENGTH_LONG)
								.show();

						((MyApp) getApplication()).putUserName(username);

						Intent i = new Intent(Register.this, Login.class);
						i.putExtra("username", username);
						i.putExtra("password", password);
						startActivity(i);
						finish();
					} else {
						Toast.makeText(Register.this, "网络异常，注册失败",
								Toast.LENGTH_LONG).show();
					}

				};

				@Override
				protected Object doInBackground(Void... params) {
					try {
						return mWebServiceClient.sysRegister(username,
								password, phone, phone, type);
					} catch (IOException e) {
						return e;
					} catch (XmlPullParserException e) {
						return e;
					}
				}
			}.execute();
			break;
		case R.id.active:
			startActivity(new Intent(this, UserActive.class));
			finish();
			break;
		}
	}
}
